package com.android.RssReader.Screen;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

import com.android.RssReader.R;
import com.android.RssReader.RssReaderApplication;

public class ScreenSettings extends PreferenceActivity implements IScreen,
OnPreferenceChangeListener {
	SharedPreferences sp; 
	private ListPreference fontSP;
	private ListPreference historySP;
	private EditTextPreference ipSP;
	private EditTextPreference portSP;
	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		String preferenceKey = preference.getKey();
		if (preferenceKey.equals("font_size_value")) {
			int value = Integer.parseInt(newValue.toString());
	        RssReaderApplication.LogD(ScreenSettings.class, "font value:" + value);
	        sp.edit().putInt("font_size_value", value).commit();
		} else if (preferenceKey.equals("delete_history_value")){
			int value = Integer.parseInt(newValue.toString());
	        RssReaderApplication.LogD(ScreenSettings.class, "history value:" + value);
	        sp.edit().putInt("delete_history_value", value).commit();
		} else if (preferenceKey.equals("server_ip_edit")){
			String value = (String)newValue;
	        RssReaderApplication.LogD(ScreenSettings.class, "ip value:" + value);
	        sp.edit().putString("server_ip_edit", value).commit();
		}  else if (preferenceKey.equals("server_port_edit")){
			String value = (String)newValue;
	        RssReaderApplication.LogD(ScreenSettings.class, "port value:" + value);
	        sp.edit().putString("server_port_edit", value).commit();
		}
		return false;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.software_parameters_setting);
		sp = PreferenceManager.getDefaultSharedPreferences(this);
		fontSP = (ListPreference) findPreference("font_size_value");
		fontSP.setOnPreferenceChangeListener(this);
		
		historySP = (ListPreference) findPreference("delete_history_value");
		historySP.setOnPreferenceChangeListener(this);
		
		ipSP = (EditTextPreference) findPreference("server_ip_edit");
		ipSP.setOnPreferenceChangeListener(this);
		
		portSP = (EditTextPreference) findPreference("server_port_edit");
		portSP.setOnPreferenceChangeListener(this);
	}

	@Override
	public boolean haveMenu() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SCREEN_TYPE getType() {
		return SCREEN_TYPE.SCREEN_SETTINGS_T;
	}

}
